package com.sun.designPattern.Observer.ProjectImp;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;

public class EntityChangeEvent {
	public static final int TYPE_ADD = 1;
	public static final int TYPE_UPDATE = 2;
	public static final int TYPE_DELETE = 3;

	private Class<?> entityCls;
	private Serializable id;
	private int type;

	public EntityChangeEvent(Class<?> entityCls, Serializable id, int type) {
		this.id = id;
		this.type = type;
		this.entityCls = entityCls;
	}

	public Class<?> getEntityCls() {
		return entityCls;
	}

	public Serializable getId() {
		return id;
	}

	public String getIdStr() {
		return id != null ? id.toString() : null;
	}

	public void setId(Serializable id) {
		this.id = id;
	}

	public int getType() {
		return type;
	}

	@Override
	public boolean equals(Object obj) {
		if (!EntityChangeEvent.class.isInstance(obj)) {
			return false;
		}
		EntityChangeEvent otherObj = (EntityChangeEvent) obj;
		return (StringUtils.equals(getIdStr(), otherObj.getIdStr()));
	}

	@Override
	public int hashCode() {
		if (id != null) {
			return id.hashCode();
		}
		return super.hashCode();
	}

}
